<?php
	class infoListModel extends Model{

		private $ids;//子分类id字符串

		/**
		 * 获得当前位置
		 * @return [type] [description]
		 */
		function getPosition($lid,$id){
		
			if(!$lid && !$id) return;

			if($id && !$lid){
				$info = $this->Table('info')->field('lid,title')->where("id=$id")->one();
				$lid = $info?$info['lid']:$lid;
			}
			$lids = $this->getLids($lid,1);

			$larr = explode(",", $lids);
			$str = '';
			$str .='<a href="'.__ROOT__.'">首页</a>';
			if(is_array($larr)){
				foreach($larr as $v){
					$result = $this->field('id,name')->where("id=$v")->one();
					if($result){
						$str .= ' > <a href="'.__APP__.'/Info/index/lid/'.$result['id'].'">'.$result['name'].'</a>';
					}
				}
			}

			if(isset($info) && $id){
				$str .=' > '.$info['title'];
			}
			return $str;
		}
		function getLids($lid,$frompath=0){
				if($frompath){
					$result = $this->field('path')->where("id=$lid")->one();
					if($result){
						$ipath = $result['path'].'-'.$lid;
						$ipath = ltrim($ipath,'0-');
						$ipath = str_replace("-", ',', $ipath);
						return $ipath;
					}
				}
				$this->getId($lid);
				$str = $lid.','.trim($this->ids,',');
				return trim($str,',');

		}
		function getId($id){
			$data = $this->field('id')->where("pid=$id")->one();
			if($data){
				$this->ids .=$data['id'].',';
				$this->getId($data['id']);
			}
		}
		/**
		 * 返回栏目分类数组
		 * @param  integer $pid   [description]
		 * @param  integer $level [description]
		 * @return [type]         [description]
		 */
		function navList($pid=0,$level=2){
			$one = $this->where('pid = '.$pid)->field('id,pid,name')->order('orders')->findall();
			
			// 显示子栏目
			var_dump($one);
			if($level>1){
				foreach($one as $k1=>$v1){
					// 查找二级菜单
					$list = $this->where('pid = '.$v1['id'])->field('id,pid,name')->order('orders')->findall();
					// 赋值二级菜单
					$one[$k1]['child'] = $list?$list:0;
					// 如果没有二级菜单，则查询当前一级菜单内容
					if(!$one[$k1]['child']){
						$two_info = $this->query("select id as aid ,title as name from ica_info where lid=".$v1['id'].' and isshow=1 order by orders limit 6');
						// var_dump($two_info);die;
						// 将文章标题赋值二级菜单
						$one[$k1]['child'] = $two_info;
					}

					// 查找三级菜单
					if($level==3 && $list){
						// p($one[$k1]['child']);
						// exit;
						if(is_array($one[$k1]['child'])){
							foreach($one[$k1]['child'] as $k2=>$v2){
								$one[$k1]['child'][$k2]['child'] = $this->where('pid = '.$v2['id'])->field('id,pid,name')->order('orders')->findall();
							}
						}

					}

				}

			}
			
			return $one;



			
		}
		//获得分类下拉列表项字符串 传入第二个参数则此id为选中
		function getList($arr='',$id='',$sel=''){
			if(!$arr){//zt为真，则
				$sql = "select `id`,`name`,`path`,concat(`path`,'-',`id`) as ipath from ica_infoList where isshow=1 order by ipath";
				$arr = $this->query($sql);
			}

			$str='';
			if($id){//存在id
				foreach($arr as $v){
					if($v['id']==$id){
						$str.="<option value='".$v['id']."' selected='selected'>";
					}else{
						$str.="<option value='".$v['id']."'>";
					}
				
				$str.="|-";
				$num = count(explode('-',$v['path']));
				$str.=str_repeat('-',$num);
				$str.=$v['name'];
				$str.='</option>\r\n';
				}	
			
			}else{//没有传id
				if($sel){//返回带selectjs判断字符串
					foreach($arr as $v){
						if(isset($_GET['fenlei']) &&$_GET['fenlei']==$v['id']){
							$str.="<option value='".$v['id']."' selected='selected'>";
						}else{
							$str.="<option value='".$v['id']."'>";
						}
					
					$str.="|-";
					$num = count(explode('-',$v['path']));
					$str.=str_repeat('-',$num);
					$str.=$v['name'];
					$str.='</option>\r\n';
					}
				}else{
					foreach($arr as $v){
					$str.="<option value='".$v['id']."'>";
					$str.="|-";
					$num = count(explode('-',$v['path']));
					$str.=str_repeat('-',$num);
					$str.=$v['name'];
					$str.='</option>\r\n';
					}
				}
						
			}
			
			return $str;
		}


	}
?>